package com.tfjybj.itoo.teach.provider.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.entity.YearSemesterEntity;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.TeachClassEntity;
import com.tfjybj.itoo.teach.model.CourseModel;
import com.tfjybj.itoo.teach.model.FaceToFaceModel;
import com.tfjybj.itoo.teach.model.StudentModel;
import com.tfjybj.itoo.teach.model.TeachClassModel;
import com.tfjybj.itoo.teach.provider.service.CourseService;
import com.tfjybj.itoo.teach.provider.service.ElectiveCoursePubService;
import com.tfjybj.itoo.teach.provider.service.TeachClassService;
import io.swagger.annotations.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * TeachClassController
 * teachClass表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"teachClass表接口"})
@RequestMapping(value = "/teachClass")
@RestController
public class TeachClassController {

    @Resource
    private TeachClassService teachClassService;
    @Resource
    private ElectiveCoursePubService electiveCoursePubService;
    @Resource
    private CourseService courseService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model TeachClassModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody TeachClassModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        //校验上课班code和课程id
        if(!teachClassService.findTeachClassByCodeAndCourseId(model.getCode(),model.getCourseId()).isEmpty()){
            return ItooResult.build(ItooResult.FAIL, "上课班代码重复");
        }
        TeachClassEntity teachClassEntity = new TeachClassEntity();
        teachClassEntity.setCapacity(Integer.parseInt(model.getCapacity()));
        teachClassEntity.setCourseId(model.getCourseId());
        teachClassEntity.setCode(model.getCode());
        teachClassEntity.setSchoolYear(model.getSchoolYear());
        teachClassEntity.setName(model.getName());
        teachClassEntity.setIsDelete(0);
        teachClassEntity.setId(IdWorker.getIdStr());
        teachClassService.save(teachClassEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        teachClassService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        teachClassService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model TeachClassModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改teachClass")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody TeachClassModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        //校验上课班code和课程id
        if(!teachClassService.findTeachClassByCodeAndCourseId(model.getCode(),model.getCourseId()).isEmpty()){
            return ItooResult.build(ItooResult.FAIL, "上课班代码重复");
        }
        //根据名称查询
        TeachClassEntity teachClassEntity = new TeachClassEntity();
        teachClassEntity.setCapacity(Integer.parseInt(model.getCapacity()));
        teachClassEntity.setCourseId(model.getCourseId());
        teachClassEntity.setCode(model.getCode());
        teachClassEntity.setSchoolYear(model.getSchoolYear());
        teachClassEntity.setName(model.getName());
        teachClassEntity.setIsDelete(0);
        teachClassEntity.setId(model.getId());
        Boolean flag= teachClassService.updateById(teachClassEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功",flag);
    }

    /**
     * 根据id查找TeachClass
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        TeachClassEntity teachClassEntity = teachClassService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teachClassEntity);
    }

    /**
     * 分页查询所有TeachClass
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有TeachClass")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<TeachClassEntity> teachClasses = teachClassService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teachClasses);
    }

    /**
     * 根据上课班名称模糊查询teachClass
     *
     * @param name  上课班名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的teachClass
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据上课班名称模糊查询teachClass", notes = "分页根据上课班名称模糊查询teachClass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryTeachClassByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryTeachClassByLikeName(
            @RequestParam(required = false, defaultValue = "") String name,
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize) {
        PageInfo<TeachClassModel> teachClassList = teachClassService.queryByLikeName(name, pageNo,
                pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teachClassList);
    }
    //endregion








    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 分页根据关键词模糊查询上课班code和上课班名称
     *
     * @param teachClassCode 上课班code
     * @param teachClassName 上课班名称
     * @param pageNo 页码
     * @param pageSize 页数
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-19 21:08:06
     */
    @ApiOperation(value = "分页根据关键词模糊查询上课班code和上课班名称")
    @GetMapping(value = "queryTeachClassInfoByCodeAndNameStrLike/{pageNo}/{pageSize}")
    public ItooResult queryTeachClassInfoByCodeAndNameStrLike(
            @RequestParam(required = false, defaultValue = " ") String teachClassCode,
            @RequestParam(required = false, defaultValue = " ") String teachClassName,
            @RequestParam(required = false, defaultValue = " ") String schoolYear,
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1")
            @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "页数", required = true, example = "10")
            @PathVariable Integer pageSize) {
        PageInfo<TeachClassModel> teachClassModelPageInfo = teachClassService.queryTeachClassInfoByCodeAndNameStrLike(teachClassCode,teachClassName,schoolYear,pageNo,pageSize);

        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teachClassModelPageInfo);
    }



    /**
     * 导出-根据代码名称上课班模糊查询
     *
     * @param response  回复
     * @param code 上课班code
     * @param name  上课班名称
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2 2018-11-23 16:32:15
     */
    @ApiOperation(value = "导出-根据代码名称上课班模糊查询")
    @GetMapping(value = "exportFile/teachClassExport")
    public ItooResult teachClassExport(
            HttpServletResponse response,
            @RequestParam(required = false, defaultValue = " ") String code,
            @RequestParam(required = false, defaultValue = " ") String name){
        return teachClassService.teachClassExport(response,code,name);
    }
    /**
     * 查询学年学期
     *
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2 2018-11-19 15:51:55
     */
    @ApiOperation(value = "查询学年学期")
    @GetMapping(value = "querySchoolYear")
    public ItooResult querySchoolYear( ) {

        List<YearSemesterEntity> yearSemesterEntities = electiveCoursePubService.querySemester();
        return ItooResult.build(ItooResult.SUCCESS, "查询学年学期成功",yearSemesterEntities);
    }
    /**
     * 根据上课班ID查询对应的剩余容量
     *
     * @param teachClassId 上课班ID
     * @return author 严文文
     * @since 2.0.0 2 2018-11-14 14:41:39
     */
    @ApiOperation(value = "根据上课班ID查询对应的剩余容量")
    @GetMapping(value = "queryRemCapacityByTeachClassId/{teachClassId}")
    public ItooResult queryRemCapacityByTeachClassId(
            @ApiParam(value = "上课班Id", required = true) @PathVariable String teachClassId) {
        int result;
        if (StringUtils.isEmpty(teachClassId)) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId");
        }
        result = teachClassService.queryRemCapacityByTeachClassId(teachClassId);
        return ItooResult.build(ItooResult.SUCCESS, "根据上课班ID查询对应的剩余容量成功",result);
    }

    /**
     * 根据上课班code查询code是否存在
     *
     * @param teachClassCode 上课班code
     * @return  ItooResult
     * @author 严文文
     * @since 2.0.0 2 2018-11-19 15:51:55
     */
    @ApiOperation(value = "根据上课班code查询code是否存在")
    @GetMapping(value = "queryIsExitTeachClassCode/{teachClassCode}")
    public ItooResult queryIsExitTeachClassCode(
            @ApiParam(value = "上课班Code", required = true) @PathVariable String teachClassCode) {
        Integer result;
        if (StringUtils.isEmpty(teachClassCode)) {
            return ItooResult.build(ItooResult.FAIL, "teachClassCode");
        }
        result = teachClassService.queryIsExitTeachClassCode(teachClassCode);
        return ItooResult.build(ItooResult.SUCCESS, "上课班code查询code成功",result);
    }

    /**
     * 根据上课班名称查询该名称是否存在
     *
     * @param teachClassName 上课班名称
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2 2018-11-19 15:51:55
     */
    @ApiOperation(value = "根据上课班名称查询该名称是否存在")
    @GetMapping(value = "queryIsExitTeachClassName/{teachClassName}")
    public ItooResult queryIsExitTeachClassName(
            @ApiParam(value = "上课班名称", required = true) @PathVariable String teachClassName) {
        String result;
        if (StringUtils.isEmpty(teachClassName)) {
            return ItooResult.build(ItooResult.FAIL, "teachClassName");
        }
        result = teachClassService.queryIsExitTeachClassName(teachClassName);
        return ItooResult.build(ItooResult.SUCCESS, "根据上课班ID查询对应的剩余容量成功",result);
    }

    /**
     * 根据上课班ID更新对应的剩余容量
     *
     * @param teachClassId 上课班ID
     * @param remCapacity 剩余容量
     * @return ItooResult
     * @return author 严文文
     * @since 2.0.0 2 2018-11-14 14:41:39
     */
    @ApiOperation(value = "根据上课班ID更新对应的剩余容量")
    @PutMapping(value = "updateRemCapacityByTeachClassId/{teachClassId}/{remCapacity}")
    public ItooResult updateRemCapacityByTeachClassId(
            @ApiParam(value = "上课班Id", required = true) @PathVariable String teachClassId,
            @ApiParam(value = "剩余容量", required = true) @PathVariable int remCapacity) {
        int result;
        if (StringUtils.isEmpty(teachClassId)) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId");
        }
        if (StringUtils.isEmpty(remCapacity)) {
            return ItooResult.build(ItooResult.FAIL, "remCapacity");
        }
        result = teachClassService.updateRemCapacityByTeachClassId(teachClassId,remCapacity);
        return ItooResult.build(ItooResult.SUCCESS, "根据上课班ID查询对应的剩余容量成功",result);
    }
    /**
     *根据上课班id查询学生相关信息
     *
     * @param teachClassId 上课班id
     * @param pageNum 页码
     * @param pageSize 页数
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-17 22:05:23
     */
    @ApiOperation(value = "根据上课班id查询学生相关信息", notes = "queryStudentInfoByClassId(teachClassId, pageNum, pageSize)")
    @GetMapping(value = "/queryStudentInfoByTeachClassId/{teachClassId}/{pageNum}/{pageSize}")
    public ItooResult queryStudentInfoByTeachClassId(@ApiParam( value = "上课班id", required = true) @PathVariable String teachClassId,
                                                     @ApiParam( value = "页码", required = true, example = "1") @PathVariable Integer pageNum,
                                                     @ApiParam( value = "每页记录数", required = true, example = "10") @PathVariable Integer pageSize) {

        if (StringUtils.isEmpty(teachClassId)){
            return ItooResult.build(ItooResult.FAIL, "classId");
        }
        PageInfo<StudentModel> studentModel = teachClassService.queryStudentInfoByTeachClassId(teachClassId, pageNum, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", studentModel);
    }

    /**
     * 下载上课班信息模板，批量导入使用
     *
     * @param response 回复
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-25 16:51:36
     */
    @ApiOperation(value = "下载上课班信息模板，批量导入使用", notes = "下载上课班信息模板，批量导入使用")
    @GetMapping(value = {"/downLoadTeachClassTemplate"})
    public ItooResult downLoadTeachClassTemplate(HttpServletResponse response) {
        if (teachClassService.downLoadTeachClassTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载上课班信息模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载上课班信息模板失败");
        }
    }

    /**
     * 批量导入上课班信息
     * @param multipartFile 多文件
     * @param request 请求
     * @param response 回复
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-25 16:51:36
     */
    @ApiOperation(value = "批量导入上课班信息", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importTeachClassByTemplate"})
    public ItooResult importTeachClassByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                 HttpServletRequest request, HttpServletResponse response) {
        return teachClassService.importTeachClassByTemplate(multipartFile, request, response);
    }


    /**
     * 根据ID导出上课班信息
     *
     * @param response 回复
     * @param idList 上课班id集合
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-25 16:51:36
     */
    @ApiOperation(value = "根据ID导出上课班信息")
    @GetMapping(value = {"/exportAllByIds"})
    public ItooResult exportAllByIds(HttpServletResponse response, @RequestParam(required = false,defaultValue = "") List<String> idList) {
        return teachClassService.exportAllByIds(response, idList);
    }



    /**
     * 根据上课班ID查询上课班详情
     *
     * @param idList 上课班ID
     * @return 上课班实体集合
     * @author 严文文
     * @since 2.0.0 2018-11-28 09:15:39
     */
    @ApiOperation(value = "根据上课班ID查询上课班详情")
    @GetMapping(value = "/queryTeachClassInfoByIds/{idList}")
    public ItooResult queryTeachClassInfoByIds (@ApiParam(name = "id", value = "上课班ID",required = true) @PathVariable List<String> idList){
        List<TeachClassModel> teachClassModel = teachClassService.queryTeachClassInfoByIds(idList);
        return ItooResult.build(ItooResult.SUCCESS,"查询公选课培养计划成功!",teachClassModel);
    }

    /**
     * 根据条件导出上上课班信息
     *
     * @param response 回复
     * @param schoolYear 学期学年
     * @param code 上课班code
     * @param name 上课班名称
     * @return ItooResult
     * author 严文文
     * @since 2.0.0 2018-11-28 09:15:39
     */
    @ApiOperation(value = "根据条件导出上上课班信息")
    @GetMapping(value = {"/exportByStrLike"})
    public ItooResult exportByStrLike(HttpServletResponse response,
                                      @RequestParam(required = false,defaultValue = "") String schoolYear,
                                      @RequestParam(required = false,defaultValue = "") String code,
                                      @RequestParam(required = false,defaultValue = "") String name
    ) {
        return teachClassService.exportByStrLike(response,schoolYear,code,name);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return ItooResult
     * @author 严文文
     * @since 2018-12-2 15:08:35
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (teachClassService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }
    }
    /**
     * 分页查询所有TeachClass,包括课程code和name
     *
     * @author 严文文
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有TeachClass")
    @GetMapping(value = {"/queryAll/{pageNo}/{pageSize}"})
    public ItooResult queryAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                               @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<TeachClassModel> teachClass = teachClassService.queryAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teachClass);
    }


    /**
     * 根据上课班code和课程id查询学生相关信息
     *
     * @param code 上课班code
     * @param courseId 课程id
     * @return ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据上课班id查询学生相关信息", notes = "findTeachClassByCodeAndCourseId(code, courseId)")
    @GetMapping(value = "/findTeachClassByCodeAndCourseId/{code}/{courseId}")
    public ItooResult findTeachClassByCodeAndCourseId(@ApiParam( value = "上课班代码", required = true) @PathVariable String code,
                                                      @ApiParam( value = "课程id", required = true) @PathVariable String  courseId) {
        if (StringUtils.isEmpty(code)){
            return ItooResult.build(ItooResult.FAIL, "code");
        }
        if (StringUtils.isEmpty(courseId)){
            return ItooResult.build(ItooResult.FAIL, "courseId");
        }
        List<TeachClassModel>  result =teachClassService.findTeachClassByCodeAndCourseId( code, courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", result);
    }

    /**
     * 根据学院ID查询所有课程
     *
     * @param academy  学院ID
     * @return  ItooResult
     * @author 严文文
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据学院ID查询所有课程")
    @GetMapping(value = "/queryCourseByAcademy/{academy}")
    public ItooResult queryCourseByAcademy(@ApiParam(name = "academy", value = "学院ID", required = true) @PathVariable String academy) {
        List<CourseModel> courseModels = courseService.queryCourseByacademyId(academy) ;
        return ItooResult.build(ItooResult.SUCCESS, "查询非公选课信息成功！", courseModels);
    }

    /**
     * 面对面建组
     * @param faceToFaceModel 分组学生实体
     * @return ItooResult 组内人员信息
     * @author 郄子硕
     * @since  2019-2-24 15:44:27
     */
    @ApiOperation(value = "面对面结组新接口")
    @PostMapping(value = {"/faceToFaceAddClass"})
    public ItooResult faceToFaceAddClass(@RequestBody FaceToFaceModel faceToFaceModel){
        return teachClassService.faceToFaceAddClass(faceToFaceModel);
    }

    /**
     * 确认分组后，获取组id
     * @return ItooResult
     * @author 郄子硕
     * @since  2019-2-24 15:44:27
     */
    @ApiOperation(value = "面对面结组--添加分组")
    @GetMapping(value = {"/insertClassInfo"})

    public ItooResult insertClassInfo(){
        return teachClassService.insertClassInfo();
    }

    /**
     * 填写分组信息，更新分组信息
     * @return ItooResult
     * @author 郄子硕
     * @since  2019-2-24 15:44:27
     */
    @ApiOperation(value = "面对面结组--更新分组信息")
    @PostMapping(value = {"/updateClassInfo"})
    public ItooResult updateClassInfo(@RequestBody TeachClassEntity teachClassEntity){
        return teachClassService.updateClassInfo(teachClassEntity);
    }

    /**
     * 确认建组，将组内所有人员添加到库里
     * @param faceToFaceModel
     * @return ItooResult
     * @author 郄子硕
     * @since  2019-2-24 15:44:27
     */
    @ApiOperation(value = "面对面结组--添加组内成员信息")
    @PostMapping(value = {"/confirmCreateClass"})
    public ItooResult confirmCreateClass(@RequestBody FaceToFaceModel faceToFaceModel){
        return teachClassService.confirmCreateClass(faceToFaceModel);
    }


}    
